package com.javaweb.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.*;
import com.javaweb.admin.mapper.KdlSineGoodsmovementBMapper;
import com.javaweb.admin.mapper.KdlSineGoodsmovementMapper;
import com.javaweb.admin.mapper.KdlSineSoOutboundBMapper;
import com.javaweb.admin.mapper.KdlSineSoOutboundMapper;
import com.javaweb.admin.query.KdlSineGoodsmovementQuery;
import com.javaweb.admin.query.KdlSineSoOutboundQuery;
import com.javaweb.admin.service.IKdlSineSoOutboundService;
import com.javaweb.admin.vo.kdlsinegoodsmovement.KdlSineGoodsmovementListVo;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.system.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class KdlSineSoOutboundServiceImpl extends BaseServiceImpl<KdlSineSoOutboundMapper, KdlSineSoOutbound> implements IKdlSineSoOutboundService {

    @Autowired
    private KdlSineSoOutboundMapper kdlSineSoOutboundMapper;

    @Autowired
    private KdlSineSoOutboundBMapper kdlSineSoOutboundBMapper;

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        KdlSineSoOutboundQuery kdlSineSoOutboundQuery = (KdlSineSoOutboundQuery) query;
        // 查询条件
        QueryWrapper<KdlSineSoOutbound> queryWrapper = new QueryWrapper<>();
        // queryWrapper.orderByDesc("id");

        // 获取数据列表
        IPage<KdlSineSoOutbound> page = new Page<>(kdlSineSoOutboundQuery.getPage(), kdlSineSoOutboundQuery.getLimit());
        IPage<KdlSineSoOutbound> pageData = kdlSineSoOutboundMapper.selectPage(page, queryWrapper);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult create(KdlSineSoOutboundDto dto) {
        try
        {
            KdlSineSoOutbound entity = dto.getSooutbound();
            String orderDate = DateUtil.format(DateUtil.parseDate(entity.getOrderdate()), DateUtils.YYYY_MM_DD);
            entity.setOrderdate(orderDate);
            entity.setPrincipalid(CommonConfig.kdlCompanyId);
            entity.setStatus(Constants.KDL_TO_SUBMIT_STATUS);
            entity.setSalesorderno(kdlSineSoOutboundMapper.getOrderNo());
            kdlSineSoOutboundMapper.insert(entity);
            for (KdlSineSoOutboundB detail:dto.getSooutboundLines()) {
                detail.setPid(entity.getId());
                detail.setSolineno(DateUtils.dateTimeNow());
                kdlSineSoOutboundBMapper.insert(detail);
            }
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error(ex.getMessage());
        }
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult update(KdlSineSoOutboundDto dto) {
        try
        {
            KdlSineSoOutbound entity = dto.getSooutbound();
            kdlSineSoOutboundMapper.updateById(entity);
            //删除之前的明细
            Map<String,Object> param = new HashMap<>();
            param.put("pid",entity.getId());
            kdlSineSoOutboundBMapper.deleteByMap(param);

            for (KdlSineSoOutboundB detail:dto.getSooutboundLines()) {
                detail.setPid(entity.getId());
                kdlSineSoOutboundBMapper.insert(detail);
            }
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error(ex.getMessage());
        }
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult deleteById(Integer id) {
        try
        {
            kdlSineSoOutboundMapper.deleteById(id);
            //删除明细
            Map<String,Object> param = new HashMap<>();
            param.put("pid",id);
            kdlSineSoOutboundBMapper.deleteByMap(param);
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error(ex.getMessage());
        }

    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult getDetailList(BaseQuery query) {
        KdlSineSoOutboundQuery kdlSineSoOutboundQuery = (KdlSineSoOutboundQuery) query;
        return JsonResult.success(kdlSineSoOutboundMapper.getOutboundDetail(((KdlSineSoOutboundQuery) query).getId()));
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult getOutBoundList(BaseQuery query) {
        KdlSineSoOutboundQuery kdlSineSoOutboundQuery = (KdlSineSoOutboundQuery) query;
        // 查询条件
        // 获取数据列表
        IPage<KdlSineSoOutbound> page = new Page<>(kdlSineSoOutboundQuery.getPage(), kdlSineSoOutboundQuery.getLimit());
        IPage<KdlSineSoOutbound> pageData = kdlSineSoOutboundMapper.getOutBoundList(page, kdlSineSoOutboundQuery);
        return JsonResult.success(pageData);
    }
}
